﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern
{
    //被觀察者
    public abstract class Subject
    {
        //定義一個觀察者數組
        private List<Observer> _observers = new List<Observer>();

        /// <summary>
        /// 增加一個觀察者
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(Observer observer)
        {
            _observers.Add(observer);
        }

        /// <summary>
        /// 刪除一個觀察者
        /// </summary>
        /// <param name="observer"></param>
        public void Detach(Observer observer)
        {
            _observers.Remove(observer);
        }

        /// <summary>
        /// 通知所有觀察者
        /// </summary>
        public string Notify()
        {
            string str = string.Empty;
            foreach (var o in _observers)
            {
                str += o.Update()+ "\n";
            }
            return str;
        }
    }
}
